package main.process;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

import main.request.RequestCdup;


public class FtpRequest extends Thread{

	private Socket clientSocket;
	private Socket dataSocket;
	private BufferedReader reader;
	private DataOutputStream writer;
	private DataInputStream dataReader;
	private DataOutputStream dataWriter;
	private String nameUser;
	private Boolean connected;
	private String pathName;
	private boolean passive;
	private ServerSocket passiveServer;
	private int port;
	private String root;
	private static boolean debug = false;

	public FtpRequest(Socket s)
	{
		
		this.connected = true;
		this.nameUser = "";
		this.pathName = System.getProperty("user.dir");
		this.root = this.pathName;
		this.setPassive(false);
		this.setClientSocket(s);
		try {
			reader = new BufferedReader(new InputStreamReader(s.getInputStream()));
			writer = new DataOutputStream(s.getOutputStream());
			write("220 Welcome, please login\n");

			while(this.connected) {
				processRequest();
			}
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}

	public void processRequest() {
		String line;
		
		
		try {
			while ((line = reader.readLine()) != null) 
			{
				String[] command = line.split("\\s");
				if (command.length > 0) command[0] = command[0].toUpperCase();
				try {
					RequestCdup.getInstance().processRequest(command, this);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				if(!getConnected()) break;
			}
		} catch (IOException e) {
			System.err.println("Erreur readLine reader");
			e.printStackTrace();
		}
	}


	
	public String getNameUser() 
	{
		return nameUser;
	}

	public void setNameUser(String nameUser) 
	{
		this.nameUser = nameUser;
	}


	public void myFlush() throws IOException 
	{
		this.dataWriter.flush();

	}

	public String getPathName() 
	{
		return this.pathName;
	}



	public Socket getDataSocket() {
		return dataSocket;
	}



	public void setDataSocket(Socket dataSocket) {
		this.dataSocket = dataSocket;
	}



	public DataInputStream getDataReader() {
		return dataReader;
	}



	public void setDataReader(DataInputStream dataReader) {
		this.dataReader = dataReader;
	}



	public DataOutputStream getDataWriter() {
		return dataWriter;
	}



	public void setDataWriter(DataOutputStream dataWriter) {
		this.dataWriter = dataWriter;
	}



	public void setConnectedFalse()
	{
		this.connected = false;
		
		try {
			reader.close();
		} catch (IOException e) {
			System.out.println("Erreur close reader");
			e.printStackTrace();
		}
		
		try {
			writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			this.clientSocket.close();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}	
		
	}


	public void write(String message) throws IOException
	{
		this.writer.writeBytes(message);
	}
	
	public DataOutputStream getWriter()
	{
		return this.writer;
	}

	public Socket getClientSocket() {
		return clientSocket;
	}

	public BufferedReader getReader() {
		return reader;
	}

	public void setReader(BufferedReader reader) {
		this.reader = reader;
	}

	public Boolean getConnected() {
		return connected;
	}

	public void setConnected(Boolean connected) {
		this.connected = connected;
	}

	public String getRoot() {
		return root;
	}

	public void setRoot(String root) {
		this.root = root;
	}

	public void setWriter(DataOutputStream writer) {
		this.writer = writer;
	}

	public void setClientSocket(Socket clientSocket) {
		this.clientSocket = clientSocket;
	}

	public void setPassiveServer(ServerSocket s)
	{
		this.passiveServer = s;
	}

	public void setPassiveTrue()
	{
		this.setPassive(true);
	}

	public void setPassiveFalse()
	{
		this.setPassive(false);
	}

	public void setPort(int p)
	{
		this.port = p;
	}

	public int getPort(){
		return this.port;
	}



	public boolean getPassive() 
	{
		return passive;
	}



	public void setPassive(boolean passive) {
		this.passive = passive;
	}

	public ServerSocket getPassiveServer() 
	{
		return this.passiveServer;
	}

	public boolean getDebug() {
		// TODO Auto-generated method stub
		return debug ;
	}
	
	public void setPathName(String pathName) {
		this.pathName = pathName;
	}
}